<!DOCTYPE html><html><head><title>MongoTemplate基础增删改查</title><meta charset='utf-8'><link href='https://cdn.maxiang.io/res-min/themes/marxico.css' rel='stylesheet'><style></style></head><body><div id='preview-contents' class='note-content'>
                        
                    



<h1 id="mongotemplate基础增删改查">MongoTemplate基础增删改查</h1>

<div><div class="toc"><div class="toc">
<ul>
<li><a href="#mongotemplate基础增删改查">MongoTemplate基础增删改查</a><ul>
<li><a href="#1插入操作insert">1.插入操作(insert)</a></li>
<li><a href="#2插入操作save">2.插入操作(save)</a></li>
<li><a href="#3查询精确查询和模糊查询">3.查询(精确查询和模糊查询)</a><ul>
<li><a href="#31-排序">3.1 排序</a></li>
<li><a href="#32-精确查询">3.2 精确查询</a></li>
<li><a href="#33-模糊查询">3.3 模糊查询</a></li>
<li><a href="#34-范围查询">3.4 范围查询</a></li>
<li><a href="#35-分页-limitsize-skipskipsize">3.5 分页 limit(size) skip(skipsize)</a></li>
<li><a href="#36-综合查询">3.6 综合查询</a></li>
<li><a href="#37-单条查询">3.7 单条查询</a></li>
</ul>
</li>
<li><a href="#4删除-remove">4.删除 remove()</a></li>
<li><a href="#5更新">5.更新</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>



<h2 id="1插入操作insert">1.插入操作(insert)</h2>

<pre class="prettyprint hljs-dark"><code class="hljs lasso"><div class="hljs-line"><span class="hljs-built_in">Map</span> <span class="hljs-built_in">data</span> = <span class="hljs-literal">new</span> HashMap();
</div><div class="hljs-line"><span class="hljs-built_in">data</span>.put(<span class="hljs-string">"name"</span>,<span class="hljs-string">"xiaoge"</span>);
</div><div class="hljs-line"><span class="hljs-built_in">data</span>.put(<span class="hljs-string">"sex"</span>,<span class="hljs-string">"male"</span>);
</div><div class="hljs-line"><span class="hljs-built_in">String</span> <span class="hljs-built_in">date</span> = <span class="hljs-built_in">String</span>.format(<span class="hljs-string">"%1$tF %1$tT"</span>,  <span class="hljs-literal">new</span> <span class="hljs-built_in">Date</span>());
</div><div class="hljs-line"><span class="hljs-built_in">data</span>.put(<span class="hljs-string">"date"</span>,<span class="hljs-built_in">date</span>);
</div><div class="hljs-line">mongoTemplate.insert(<span class="hljs-built_in">data</span>,<span class="hljs-string">"why"</span>);
</div></code></pre>



<h2 id="2插入操作save">2.插入操作(save)</h2>

<pre class="prettyprint hljs-dark"><code class="hljs lasso"><div class="hljs-line"> <span class="hljs-built_in">Map</span> <span class="hljs-built_in">data</span> = <span class="hljs-literal">new</span> HashMap();
</div><div class="hljs-line"> <span class="hljs-built_in">data</span>.put(<span class="hljs-string">"name"</span>,<span class="hljs-string">"xiaoge"</span>);
</div><div class="hljs-line"> <span class="hljs-built_in">data</span>.put(<span class="hljs-string">"sex"</span>,<span class="hljs-string">"male"</span>);
</div><div class="hljs-line"> <span class="hljs-built_in">String</span> <span class="hljs-built_in">date</span> = <span class="hljs-built_in">String</span>.format(<span class="hljs-string">"%1$tF %1$tT"</span>,  <span class="hljs-literal">new</span> <span class="hljs-built_in">Date</span>());
</div><div class="hljs-line"> <span class="hljs-built_in">data</span>.put(<span class="hljs-string">"date"</span>,<span class="hljs-built_in">date</span>);
</div><div class="hljs-line"> mongoTemplate.save(<span class="hljs-built_in">data</span>);
</div></code></pre>



<h2 id="3查询精确查询和模糊查询">3.查询(精确查询和模糊查询)</h2>



<h3 id="31-排序">3.1 排序</h3>

<pre class="prettyprint hljs-dark"><code class="hljs sqf"><div class="hljs-line">   query.<span class="hljs-keyword">with</span>(new <span class="hljs-built_in">Sort</span>(<span class="hljs-built_in">Sort</span>.<span class="hljs-built_in">Direction</span>.ASC,<span class="hljs-string">"date"</span>)) 
</div></code></pre>



<h3 id="32-精确查询">3.2 精确查询</h3>

<pre class="prettyprint hljs-dark"><code class="hljs stata"><div class="hljs-line">    <span class="hljs-keyword">Query</span> <span class="hljs-keyword">query</span> = new <span class="hljs-keyword">Query</span>();
</div><div class="hljs-line">    <span class="hljs-keyword">query</span>.addCriteria(Criteria.where(<span class="hljs-string">"sex"</span>).is(<span class="hljs-string">"male"</span>));
</div></code></pre>



<h3 id="33-模糊查询">3.3 模糊查询</h3>

<pre class="prettyprint hljs-dark"><code class="hljs dart"><div class="hljs-line">   <span class="hljs-built_in">Pattern</span> pattern=<span class="hljs-built_in">Pattern</span>.compile(<span class="hljs-string">"^.*"</span>+dateTime+<span class="hljs-string">".*$"</span>, <span class="hljs-built_in">Pattern</span>.CASE_INSENSITIVE);
</div><div class="hljs-line">   Query query = <span class="hljs-keyword">new</span> Query();
</div><div class="hljs-line">   query.addCriteria(Criteria.where(<span class="hljs-string">"date"</span>).regex(pattern));
</div></code></pre>



<h3 id="34-范围查询">3.4 范围查询</h3>

<pre class="prettyprint hljs-dark"><code class="hljs lasso"><div class="hljs-line">   <span class="hljs-comment">//gte : &gt;= ; lte : &lt;= ; gt : &gt; ; lt : &lt;</span>
</div><div class="hljs-line">   Query query = <span class="hljs-literal">new</span> Query();
</div><div class="hljs-line">   query.addCriteria(Criteria.<span class="hljs-keyword">where</span>(<span class="hljs-string">"date"</span>).<span class="hljs-literal">gt</span>(<span class="hljs-string">"2019-01-11 14:05:01"</span>).<span class="hljs-literal">lte</span>(<span class="hljs-string">"2019-01-11 14:09:53"</span>));
</div><div class="hljs-line">   <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Map</span>&gt; <span class="hljs-built_in">data</span> = mongoTemplate.find(query,<span class="hljs-built_in">Map</span>.class,<span class="hljs-string">"why"</span>);
</div></code></pre>



<h3 id="35-分页-limitsize-skipskipsize">3.5 分页 limit(size) skip(skipsize)</h3>

<pre class="prettyprint hljs-dark"><code class="hljs stata"><div class="hljs-line">   size:每页几条数据 ; skipsize: 跳过的数据条数
</div><div class="hljs-line">   例如:每页size条数据.则第page页的数据为   <span class="hljs-keyword">query</span>.limit(size).skip(size*(page-1))
</div><div class="hljs-line">   <span class="hljs-comment">// 默认只查询 未读数据</span>
</div><div class="hljs-line">   <span class="hljs-keyword">Query</span> <span class="hljs-keyword">query</span> = new <span class="hljs-keyword">Query</span>();
</div><div class="hljs-line">   <span class="hljs-comment">// 查询总记录数</span>
</div><div class="hljs-line">   int <span class="hljs-keyword">count</span> = (int) mongoTemplate.<span class="hljs-keyword">count</span>(<span class="hljs-keyword">query</span>, PharmacyLogModel.<span class="hljs-keyword">class</span>);
</div><div class="hljs-line">   <span class="hljs-comment">// 设置起始数</span>
</div><div class="hljs-line">   <span class="hljs-keyword">query</span>.skip((pageNum - 1 ) * pageSize);
</div><div class="hljs-line">   <span class="hljs-comment">// 设置查询条数</span>
</div><div class="hljs-line">   <span class="hljs-keyword">query</span>.limit(pageSize);
</div><div class="hljs-line">   <span class="hljs-keyword">List</span>&lt;PharmacyLogModel&gt; <span class="hljs-keyword">list</span> = mongoTemplate.find(<span class="hljs-keyword">query</span>,PharmacyLogModel.<span class="hljs-keyword">class</span>);
</div><div class="hljs-line">   <span class="hljs-comment">// 返回的是一个PageInfo,包含了分页的所有信息</span>
</div><div class="hljs-line">   PageInfo&lt;PharmacyLogModel&gt; pageInfo = new PageInfo(<span class="hljs-keyword">list</span>);
</div><div class="hljs-line">   <span class="hljs-comment">// --数据</span>
</div><div class="hljs-line">   pageInfo.setList(<span class="hljs-keyword">list</span>);
</div><div class="hljs-line">   <span class="hljs-comment">// --页数</span>
</div><div class="hljs-line">   pageInfo.setPageNum(pageNum);
</div><div class="hljs-line">   <span class="hljs-comment">// -- 条数</span>
</div><div class="hljs-line">   pageInfo.setPageSize(pageSize);
</div><div class="hljs-line">   <span class="hljs-comment">// -- 总页数</span>
</div><div class="hljs-line">   pageInfo.setPages(<span class="hljs-keyword">count</span> % pageSize == 0 ? 1 : <span class="hljs-keyword">count</span> / pageSize + 1);
</div><div class="hljs-line">   <span class="hljs-comment">// -- 当前页个数</span>
</div><div class="hljs-line">   pageInfo.setSize(<span class="hljs-keyword">list</span>.size());
</div><div class="hljs-line">   <span class="hljs-comment">// -- 总记录数</span>
</div><div class="hljs-line">   pageInfo.setTotal(<span class="hljs-keyword">count</span>);
</div></code></pre>



<h3 id="36-综合查询">3.6 综合查询</h3>

<pre class="prettyprint hljs-dark"><code class="hljs lasso"><div class="hljs-line">   Query query = <span class="hljs-literal">new</span> Query();
</div><div class="hljs-line">   query.<span class="hljs-keyword">with</span>(<span class="hljs-literal">new</span> Sort(Sort.Direction.ASC,<span class="hljs-string">"date"</span>));
</div><div class="hljs-line">   Pattern pattern=Pattern.compile(<span class="hljs-string">"^.*"</span>+<span class="hljs-string">"y"</span>+<span class="hljs-string">".*$"</span>, Pattern.CASE_INSENSITIVE);
</div><div class="hljs-line">   query.addCriteria(Criteria.<span class="hljs-keyword">where</span>(<span class="hljs-string">"sex"</span>).is(<span class="hljs-string">"male"</span>).<span class="hljs-literal">and</span>(<span class="hljs-string">"name"</span>).regex(pattern).
</div><div class="hljs-line">   <span class="hljs-literal">and</span>(<span class="hljs-string">"date"</span>).<span class="hljs-literal">gt</span>(<span class="hljs-string">"2019-01-11 14:05:01"</span>).<span class="hljs-literal">lte</span>(<span class="hljs-string">"2019-01-11 14:09:53"</span>));
</div><div class="hljs-line">   <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Map</span>&gt; <span class="hljs-built_in">data</span> = mongoTemplate.find(query,<span class="hljs-built_in">Map</span>.class,<span class="hljs-string">"why"</span>);
</div></code></pre>



<h3 id="37-单条查询">3.7 单条查询</h3>

<pre class="prettyprint hljs-dark"><code class="hljs armasm"><div class="hljs-line">   <span class="hljs-meta">Map</span> <span class="hljs-meta">data</span> = mongoTemplate.findOne(query,<span class="hljs-meta">Map</span>.class,<span class="hljs-string">"why"</span>)<span class="hljs-comment">;</span>
</div></code></pre>



<h2 id="4删除-remove">4.删除 remove()</h2>

<pre class="prettyprint hljs-dark"><code class="hljs stata"><div class="hljs-line">  <span class="hljs-keyword">Query</span> <span class="hljs-keyword">query</span>=new <span class="hljs-keyword">Query</span>(Criteria.where(<span class="hljs-string">"date"</span>).is(<span class="hljs-string">"2019-01-11 14:14:05"</span>));
</div><div class="hljs-line">  long <span class="hljs-keyword">count</span> = mongoTemplate.remove(<span class="hljs-keyword">query</span>,Map.<span class="hljs-keyword">class</span>,<span class="hljs-string">"why"</span>).getDeletedCount();
</div></code></pre>



<h2 id="5更新">5.更新</h2>

<pre class="prettyprint hljs-dark"><code class="hljs stata"><div class="hljs-line">  <span class="hljs-keyword">Query</span> <span class="hljs-keyword">query</span>=new <span class="hljs-keyword">Query</span>(Criteria.where(<span class="hljs-string">"date"</span>).is(<span class="hljs-string">"2019-01-11 14:13:38"</span>));
</div><div class="hljs-line">  <span class="hljs-keyword">Update</span> <span class="hljs-keyword">update</span> = <span class="hljs-keyword">Update</span>.<span class="hljs-keyword">update</span>(<span class="hljs-string">"name"</span>,<span class="hljs-string">"hhd"</span>).<span class="hljs-keyword">set</span>(<span class="hljs-string">"sex"</span>,<span class="hljs-string">"animal"</span>);
</div><div class="hljs-line">  mongoTemplate.updateFirst(<span class="hljs-keyword">query</span>, <span class="hljs-keyword">update</span>, Map.<span class="hljs-keyword">class</span>,<span class="hljs-string">"why"</span>);
</div></code></pre></div></body></html>